var JS_AREA = function(){
	var owner = this;
	owner.dom_tag = "area";
	owner.dom_search = $("#"+owner.dom_tag+"_div_search");
	owner.dom_edit = $("#"+owner.dom_tag+"_div_add_edit");
	owner.dom_table = $("#"+owner.dom_tag+"_table");
	owner.currentId = null;
	owner.currentRow = 0;
	owner.sortName = "addTime";
	owner.sortOrder = false;
	owner.map_data = {};
	owner.map_enum = {"level_1":"省份", "level_2":"城市", "level_3":"地区"};
	owner.box_level = [{text:"省份",value:1}, {text:"城市",value:2}, {text:"地区",value:3}];
	owner.isShowSearchWindow = false;
	owner.isShowEidtWindow = false;
	
	owner.dom_table.datagrid({
		onSortColumn: function(sort, order){
			owner.sortName = sort;
			owner.sortOrder = true?(order=="asc"):false;
			owner.getPage(1);
		}
	}).datagrid("getPager").pagination({
	    layout: ["list","sep","first","prev","sep","manual","sep","next","last","refresh","sep"],
	    pageList: [10,20,50,100],
	    pageSize: 20,
	    buttons: [{
	        iconCls:"icon-search",
	        handler:function(){
	            owner.dom_search.window("open").window("hcenter");
	            if(!owner.isShowSearchWindow) {
	        		$("#ipt_"+owner.dom_tag+"_search_begin").Zebra_DatePicker({
						pair: $("#ipt_"+owner.dom_tag+"_search_end")
					});
					$("#ipt_"+owner.dom_tag+"_search_end").Zebra_DatePicker({
						direction: true
					});
					$("#ipt_"+owner.dom_tag+"_search_level").combobox("loadData", owner.box_level).combobox("clear");
	            	owner.isShowSearchWindow = true;
	            }
	        }
	    },{
	        iconCls:"icon-edit",
	        handler:function(){
	        	if(owner.dom_table.datagrid("getChecked").length!=1){
	        		$.messager.alert("提示信息", "必须仅选择一条数据进行编辑！");
	        		return;
	        	}
	        	var row = owner.dom_table.datagrid("getChecked")[0];
	        	owner.currentId = row.itemId;
	        	owner.currentRow = owner.dom_table.datagrid("getRowIndex", row);
	            owner.dom_edit.window({
	            	"iconCls": "icon-edit",
	            	"title": "编辑区域"
	            }).window("open").window("hcenter");
	            owner.resetSaveOrEdit();
	        }
	    },{
	        iconCls:"icon-add",
	        handler:function(){
	            owner.currentId = null;
	            owner.dom_edit.window({
	            	"iconCls": "icon-add",
	            	"title": "添加区域"
	            }).window("open").window("hcenter");
	            owner.resetSaveOrEdit();
	        }
	    }],
	    onSelectPage: function(pageNum, pageSize){
	    	owner.getPage(pageNum, pageSize);
	    }
	}); 
	
	owner.resetSearch = function(){
		owner.dom_search.find(".easyui-textbox").textbox("clear")
		.end().find(".easyui-combobox").combobox("clear")
		.end().find(".easyui-combotree").combotree("clear")
		.end().find(".easyui-numberbox").numberbox("clear")
		.end().find(".textbox").val("");
	};
	
	owner.formatItem = function(obj){
		var item = {
			"itemId": obj.areaId,
			"p1": obj.areaCode,
			"p2": obj.areaName,
			"p3": owner.map_enum["level_"+obj.areaLevel],
			"p4": obj.areaFatherName,
			"p5": obj.areaDesc,
			"addTime": obj.addTime.date()
		};
		return item;
	};
	
	owner.getPage = function(pageNum, pageSize){
		if(pageSize==null) {
			pageSize = owner.dom_table.datagrid("getPager").pagination("options").pageSize;
		}
		var params = {};
		if(!owner.isShowSearchWindow) {
			params = {
				"searchCode": "",
				"searchName": "", 
				"searchLevel": "", 
				"searchBegin": "",
				"searchEnd": "",
				"pageNum": pageNum,
				"pageSize": pageSize,
				"orderName": owner.sortName,
				"orderFlag": owner.sortOrder
			};
		} else {
			params = {
				"searchCode": $("#ipt_"+owner.dom_tag+"_search_code").numberbox("getValue"),
				"searchName": $("#ipt_"+owner.dom_tag+"_search_name").textbox("getValue"), 
				"searchLevel": $("#ipt_"+owner.dom_tag+"_search_level").combobox("getValue"), 
				"searchBegin": $("#ipt_"+owner.dom_tag+"_search_begin").val(),
				"searchEnd": $("#ipt_"+owner.dom_tag+"_search_end").val(),
				"pageNum": pageNum,
				"pageSize": pageSize,
				"orderName": owner.sortName,
				"orderFlag": owner.sortOrder
			};
		}
		$.ajax({
			type : "get",
			url : "/manager/system/area/list",
			cache : false,
			dataType : "json",
			data: params
		}).done(function(result){
			if(result.isTrue) {
				var ps = result.ps;
				var data = new Array();
				owner.map_data = {};
				if(ps.items.length==0) {
					$.messager.alert("提示信息", "暂无数据！");
					return;
				}
				for(var i=0; i<ps.items.length; i++) {
					var obj = ps.items[i];
					owner.map_data[obj.areaId] = obj;
					data.push(owner.formatItem(obj));
				}
				owner.dom_table.datagrid("loadData", {
					"total": ps.totalCount,
					"rows": data
				}); 
			} else {
				$.messager.alert("提示信息", result.message);
			}
		}).fail(function(){
			$.messager.alert("提示信息", "网络繁忙，请稍后再试！");
		});
	};
	
	owner.resetSaveOrEdit = function(){
        if(!owner.isShowEidtWindow) {
        	$("#"+owner.dom_tag+"_ipt_p4").combotree("loadData", GLOBAL.getAreaMatchList());
        	owner.isShowEidtWindow = true;
        }
		if(owner.currentId==null) {
			owner.dom_edit.find(".easyui-textbox").textbox("clear")
			.end().find(".easyui-combobox").combobox("clear")
			.end().find(".easyui-combotree").combotree("clear")
			.end().find(".easyui-numberbox").numberbox("clear")
			.end().find(".textbox").val("");
		} else {
			var item = owner.map_data[owner.currentId];
			$("#"+owner.dom_tag+"_ipt_p1").textbox("setValue", item.areaName);
			$("#"+owner.dom_tag+"_ipt_p2").numberbox("setValue", item.areaCode);
			$("#"+owner.dom_tag+"_ipt_p3").combobox("setValue", item.areaLevel);
			$("#"+owner.dom_tag+"_ipt_p4").combotree("setValue", item.areaFather);
			$("#"+owner.dom_tag+"_ipt_p5").textbox("setValue", item.areaDesc);
		}
	};
	
	owner.saveOrEdit = function(dom){
		dom.linkbutton("disable");
		var isAllValid = new Array();
		owner.dom_edit.find(".easyui-textbox").each(function(){
			if(!$(this).textbox("isValid")){
				isAllValid.push($(this).prev("label").html());
			}
		});
		owner.dom_edit.find(".easyui-numberbox").each(function(){
			if(!$(this).numberbox("isValid")){
				isAllValid.push($(this).prev("label").html());
			}
		});
		owner.dom_edit.find(".easyui-combobox").each(function(){
			if(!$(this).combobox("isValid")){
				isAllValid.push($(this).prev("label").html());
			}
		});
		if(isAllValid.length>0){
			$.messager.alert("提示信息", isAllValid.join(",")+" 输入不正确！");
			dom.linkbutton("enable");
			return;
		}
		$.ajax({
			type : "post",
			url : "/manager/system/area/addoredit",
			cache : false,
			dataType : "json",
			data: {
				"areaId": owner.currentId,
				"areaName": 	$("#"+owner.dom_tag+"_ipt_p1").textbox("getValue"), 
				"areaCode": 	$("#"+owner.dom_tag+"_ipt_p2").numberbox("getValue"), 
				"areaLevel": 	$("#"+owner.dom_tag+"_ipt_p3").combobox("getValue"), 
				"areaFather": 	$("#"+owner.dom_tag+"_ipt_p4").combobox("getValue"), 
				"areaDesc": 	$("#"+owner.dom_tag+"_ipt_p5").textbox("getValue")
			}
		}).done(function(result){
			if(result.isTrue) {
				var item = result.tag;
				if(owner.currentId==null) {
					owner.dom_table.datagrid("appendRow", owner.formatItem(item));
				} else {
					owner.dom_table.datagrid("updateRow", {
						index: owner.currentRow,
						row: owner.formatItem(item)
					});
				}
				owner.map_data[item.areaId] = item;
				owner.dom_edit.window("close");
			} else {
				if(result.tag!=null) {
					$.messager.confirm("提示信息", "已存在该区域，并已是注销状态，是否恢复？", function(r){
						if(r) {
							owner.restore(result.tag.areaId);
						}
					});
				} else {
					$.messager.alert("提示信息", result.message);
				}
			}
		}).fail(function(){
			$.messager.alert("提示信息", "网络繁忙，请稍后再试！");
		}).complete(function(){
			dom.linkbutton("enable");
		});
	};
	
	owner.restore = function(areaId){
		$.ajax({
			type : "post",
			url : "/manager/system/area/restore",
			cache : false,
			dataType : "json",
			data: {
				"areaId": areaId
			}
		}).done(function(result){
			if(result.isTrue) {
				var item = result.tag;
				owner.dom_table.datagrid("appendRow", owner.formatItem(item));
				owner.map_data[item.areaId] = item;
			} else {
				$.messager.alert("提示信息", result.message);
			}
		}).fail(function(){
			$.messager.alert("提示信息", "网络繁忙，请稍后再试！");
		});
	};
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
};